package com.cloud.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cloud.sentinel.service.FlowServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class FlowLimitController {
    @Autowired
    private FlowServiceImpl im;

    @GetMapping("/testA")
    public String testA(){
//        try{
//            TimeUnit.MILLISECONDS.sleep(2000);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        im.test();
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB(){
        System.out.println(Thread.currentThread().getName()+"\t"+"...testB");
        return "------testB";
    }
    @GetMapping("/testD")
    public String testD(){
//        try{
//            TimeUnit.MILLISECONDS.sleep(1000);
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        int age = 10/0;
        return "------testB";
    }

    @GetMapping("/testE")
    @SentinelResource(value = "testE",fallback = "testFallback")
    public String testE(){
        int age = 10/0;
        return "------testE";

    }
    public String testFallback(){
        return "后台出现错误了";
    }

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHotKey(
            @RequestParam(value = "p1",required = false)String p1,
            @RequestParam(value = "p2",required = false)String p2
    ){
        return "-----------testHotKey";
    }
    public String deal_testHotKey(String p1, String p2, BlockException exception){
        return "-----------deal_testHotKey";//sentinel的默认提示Blocked by Sentinel (flow limiting)
    }
}
